package demo6_i;

import java.util.Arrays;
import java.util.Objects;

class Student implements Comparable<Student>{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        return this.age - o.age;
    }

    @Override
    public boolean equals(Object object) {
        if (object == null || getClass() != object.getClass()) return false;
        Student student = (Student) object;
        return age == student.age && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}

class MyValue{
    int val;
    void swap(MyValue val1, MyValue val2){
        int tmp = val1.val;
        val1.val = val2.val;
        val2.val = tmp;
    }
}

public class Test {


    public static void main(String[] args) {
        MyValue myValue1 = new MyValue();
        MyValue myValue2 = new MyValue();
        myValue1.val = 10;
        myValue2.val = 20;
        myValue2.swap(myValue1, myValue2);
        System.out.println(myValue1.val + ", " + myValue2.val);
    }

    public static void main4(String[] args) {
        Student student1 = new Student("张三", 18);
        Student student2 = new Student("张三", 18);
        System.out.println(student1.equals(student2));
    }

    //模拟冒泡排序
    //使用接口是因为统一
    public static void mySort(Comparable[] comparables){
        for (int i = 0; i < comparables.length-1; i++) {
            for (int j = 0; j < comparables.length-1-i; j++) {
                if(comparables[j].compareTo(comparables[j+1]) > 0){
                    Comparable temp = comparables[j];
                    comparables[j] = comparables[j+1];
                    comparables[j+1] = temp;
                }
            }
        }
    }

    public static void main3(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan", 18);
        students[1] = new Student("lisi", 19);
        students[2] = new Student("wangwu", 17);

        System.out.println(Arrays.toString(students));
        NameComparator nameComparator = new NameComparator();
        Arrays.sort(students, nameComparator);
        System.out.println(Arrays.toString(students));
    }

    public static void main2(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan", 18);
        students[1] = new Student("lisi", 19);
        students[2] = new Student("wangwu", 17);

//        for (Student student : students) {
//            System.out.println(student);
//        }
        System.out.println(Arrays.toString(students));
        //Arrays.sort(students);
        mySort(students);
        System.out.println(Arrays.toString(students));
//        for (Student student : students) {
//            System.out.println(student);
//        }
    }
    public static void main1(String[] args) {
        Student student1 = new Student("张三", 18);
        Student student2 = new Student("李四", 20);
        System.out.println(student1.compareTo(student2));
    }
}
